<!--
 * @Version: 1.0
 * @Date: 2021-06-28 14:56:46
 * @LastEditTime: 2022-01-27 12:09:29
 * @Description:
-->
<template>
  <div>
    <el-dialog :center="true" :title="title" :visible.sync="formVisibleDeliver" width="700px" :before-close="() => {$emit('update:formVisibleDeliver', false), $refs['ruleForm'].resetFields()}">
      <div class="from_box">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="140px" class="demo-ruleForm">
          <el-form-item :label="`已选择订单(${codes.length}个):`">
            {{ codes }}
          </el-form-item>
          <el-form-item label="请选择发货方式" prop="type">
            <el-radio-group v-model="ruleForm.type" :disabled="type==='2'?true:false" @change="changeRadio">
              <el-radio label="1">自有仓库</el-radio>
              <el-radio label="2">第三方仓库</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="ruleForm.type==='2'" label="选择第三方仓库" prop="warehouse">
            <el-select v-model="ruleForm.warehouse" placeholder="请选择">
              <el-option v-for="item in warehouseData" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div style="text-align: center;">
              <el-button type="primary" :loading="isbtn" @click="submitForm('ruleForm')">保存</el-button>
              <el-button @click="handleCancelDown">取消</el-button>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getWarehouseThrid } from '@/utils/publicData'
export default {
  props: {
    formVisibleDeliver: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    ids: {
      type: Array,
      default: () => []
    },
    codes: {
      type: Array,
      default: () => []
    },
    type: {
      type: String,
      default: '0'
    }

  },
  data() {
    return {
      isbtn: false,
      ruleForm: {
        type: '1',
        warehouse: ''
      },
      warehouseData: [],
      rules: {
        type: [
          { required: true, message: '请选择发货类型', trigger: 'blur' }
        ],
        warehouse: [
          { required: true, message: '请选择第三方仓库', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    getWarehouseThrid().then((res) => {
      this.warehouseData = res.filter((arr) => {
        return arr.is_active === 1
      })
    })
  },
  methods: {
    changeRadio(val) {
      if (val === '2') {
        getWarehouseThrid().then((res) => {
          this.warehouseData = res.filter((arr) => {
            return arr.is_active === 1
          })
        })
      }
    },
    handleCancelDown() {
      this.$emit('update:formVisibleDeliver', false)
      this.$refs['ruleForm'].resetFields()
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isbtn = true
          let parent_id = ''
          let id = ''
          this.warehouseData.map((arr) => {
            if (arr.id === this.ruleForm.warehouse) {
              parent_id = arr.parent_id
              id = arr.id
            }
          })
          const data = {
            warehouse_id: parent_id,
            warehouse_fictitious_id: id,
            type: this.ruleForm.type,
            ids: this.ids
          }
          this.orderdelivery(data)
          // this.isbtn = false
        } else {
          return false
        }
      })
    },
    orderdelivery(data) {
      const vm = this
      this.$http.post(`/deliveryOrder/ordersDeliver`, data).then((res) => {
        if (res.code === 10000) {
          this.$message.success('发货成功')
          vm.handleImportSuccess(res.data)
          vm.$emit('handleSearch')
          vm.handleCancelDown()
          this.isbtn = false
        } else {
          this.isbtn = false
        }
      }).catch(() => { this.isbtn = false })
    },
    handleImportSuccess(res) {
      // 成功
      const successNumber = res.successData ? res.successData.length : 0
      // 失败
      const repeatNumber = res.failData ? res.failData.length : 0
      // 客户费用不足
      const emptyNumber = res.userMoneyFail ? res.userMoneyFail.length : 0
      this.$notify.info({
        title: '发货结果',
        message: `发货成功${successNumber}个，失败数量${repeatNumber}个，客户费用不足数量${emptyNumber}个`
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
